window.addEvent('domready', function(){

	/**
	 * New Post
	 */
	new Pagination($(APP.newPost.pagination),{
		onPageChange: function(obj){
			$(APP.newPost.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.newPost.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','perb_tempat_kejadian','perb_keterangan','perb_status'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.newPost.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.newPost.form));
		}
	});
	
	/**
	 * approve
	 */
	new Pagination($(APP.approve.pagination),{
		onPageChange: function(obj){
			$(APP.approve.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.approve.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','perb_tempat_kejadian','perb_keterangan','perb_status'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.approve.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.approve.form));
		}
	});
	
	/**
	 * rejected
	 */
	new Pagination($(APP.reject.pagination),{
		onPageChange: function(obj){
			$(APP.reject.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.reject.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','perb_tempat_kejadian','perb_keterangan','perb_status'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.reject.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.reject.form));
		}
	});
	
	search($(APP.newPost.form), $(APP.newPost.content), $(APP.newPost.pagination));
	search($(APP.approve.form), $(APP.approve.content), $(APP.approve.pagination));
	search($(APP.reject.form), $(APP.reject.content), $(APP.reject.pagination));
	
	/**
	 * active BAP
	 */
	new Pagination($(APP.activeBAP.pagination),{
		onPageChange: function(obj){
			$(APP.activeBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.activeBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.activeBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.activeBAP.form));
		}
	});
	
	/**
	 * New BAP
	 */
	new Pagination($(APP.newBAP.pagination),{
		onPageChange: function(obj){
			$(APP.newBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.newBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.newBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.newBAP.form));
		}
	});
	
	/**
	 * Counter BAP
	 */
	new Pagination($(APP.counterBAP.pagination),{
		onPageChange: function(obj){
			$(APP.counterBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.counterBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.counterBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.counterBAP.form));
		}
	});
	
	/**
	 * Approve BAP
	 */
	new Pagination($(APP.approveBAP.pagination),{
		onPageChange: function(obj){
			$(APP.approveBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.approveBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.approveBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.approveBAP.form));
		}
	});
	
	/**
	 * Reject BAP
	 */
	new Pagination($(APP.rejectBAP.pagination),{
		onPageChange: function(obj){
			$(APP.rejectBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.rejectBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.rejectBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.rejectBAP.form));
		}
	});
	
	search($(APP.activeBAP.form), $(APP.activeBAP.content), $(APP.activeBAP.pagination));
	search($(APP.newBAP.form), $(APP.newBAP.content), $(APP.newBAP.pagination));
	search($(APP.counterBAP.form), $(APP.counterBAP.content), $(APP.counterBAP.pagination));
	search($(APP.rejectBAP.form), $(APP.rejectBAP.content), $(APP.rejectBAP.pagination));
	search($(APP.approveBAP.form), $(APP.approveBAP.content), $(APP.approveBAP.pagination));
});

function search(form, content, pagination){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			content.set('html', json.table_content);
			
			//rubah jumlah halamannya
			pagination.retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form, content){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			content.set('html', json.table_content);
		}
	}).send();
}

function redirectToView(perb_notif_id, perb_id, status){
	switch(status){
	case "SA":
	case "SI":
	case "CO":
	case "CA":
	case "CR":
		redirectToView2(perb_notif_id, perb_id, status);
		break;
	case "RE":
	case "AP":
	case "RA":
		ajax(APP.utils+'/notification/updateLook/'+perb_notif_id,'post','',$empty,
				function(res){
					document.location=APP.base+'/perbuatan/persetujuan/detailView/'+status+'/'+perb_id;
				});
	}
}

function redirectToView2(bap_notif_id, bap_id, status){
	ajax(APP.utils+'/notification/updateLookBAP/'+bap_notif_id,'post','',$empty,
		function(res){
			document.location=APP.base+'/bap/detailView/'+status+'/'+bap_id;
		});
}